package olap4cloud.util2;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Util {

	public static final long DELAYTIME_AVAIABLE = 24 * 60 * 60 * 1000;
	public static Pattern pattern=Pattern.compile("[a-z0-9\\-\\.]+\\.(com|org|net|mil|com.vn|vn|edu|(co\\.[a-z].))");
	
	/**
	 * Check avaiable session
	 * @param cookie_create : thoi gian tao cookie
	 * @param time_create : thoi gian tao log
	 * @return
	 */
	public static boolean isAvaiableSession(long cookie_create, long time_create){
		if((time_create - cookie_create) >= DELAYTIME_AVAIABLE){
			return true;
		}
		return false;
	}
	
	
	/**
	 * Chek url avaiable
	 * @param domain
	 * @param path
	 * @return
	 */
	public static boolean isAvaiableUrl (String domain, String path){
		
		Matcher matcher=pattern.matcher(domain);
		if(path.length()>3 && (matcher.find()))
			return true;
		
		return false;
	}
	
	
	/**
	 * Get previous date. Example to day is 2012/07/28 --> 2012/07/27 
	 * @return file in hadoop
	 */
	public static String getLastDate(){
		long currenttime = new java.util.Date().getTime() - 24 * 60 * 60 * 1000;
		SimpleDateFormat simple = new SimpleDateFormat("yyyy_MM_dd");
		return simple.format(new Date(currenttime));
	}
	
	public static String urldecode(String url){
		try {
			String urldecode = java.net.URLDecoder.decode(url, "UTF-8").replaceAll("http://", "");
			return urldecode.substring(0, urldecode.length() - 1);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e){
			
			e.printStackTrace();
		}
		
		return url;
		
	}
	
	public static void main(String[] args) {
		System.out.println(Util.urldecode("http%3A%2F%2Fdantri.com.vn%2F"));
		System.out.println("kenh14.vn");
	}
}
